'use strict';

var path = require('path');

var routes = function (app, express, jwt) {
    // Serving static client files
    app.use(express.static(path.join(__dirname, '../../frontend')));

    var router = require('./api')(app, express.Router(), jwt);

    app.use('/api', router);

    // 处理 AngularJS URL自动跳转
    app.use(function (req, res, next) {
        if (req.path.indexOf('/api') >= 0) {
            next();
        } else {
            var options = {
                dotfiles: 'deny',
                headers: {
                    'x-timestamp': Date.now(),
                    'x-sent': true
                }
            };

            options.root = path.join(__dirname, '../../frontend');

            var fileName = 'index.html';
            res.sendFile(fileName, options, function (err) {
                if (err) {
                    console.log(err);
                    res.status(err.status).end();
                }
            });
        }
    });
};

module.exports = routes;

